package cn.wolfcode.web.controller.audit;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.wolfcode.audit.dto.AudCarPackageDTO;
import cn.wolfcode.audit.dto.AudedCarPackageDTO;
import cn.wolfcode.audit.vo.TaskHistoryVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import cn.wolfcode.common.annotation.Log;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.enums.BusinessType;
import cn.wolfcode.audit.domain.AudCarPackage;
import cn.wolfcode.audit.service.IAudCarPackageService;
import cn.wolfcode.common.utils.poi.ExcelUtil;
import cn.wolfcode.common.core.page.TableDataInfo;

/**
 * 套餐审核Controller
 * 
 * @author zhangyf
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/audit/carPackage")
public class AudCarPackageController extends BaseController
{
    @Autowired
    private IAudCarPackageService audCarPackageService;

    /**
     * 查询套餐审核列表
     */
    @PreAuthorize("@ss.hasPermi('audit:carPackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(AudCarPackage audCarPackage)
    {
        startPage();
        List<AudCarPackage> list = audCarPackageService.selectAudCarPackageList(audCarPackage);
        return getDataTable(list);
    }

    /**
     * 导出套餐审核列表
     */
    @PreAuthorize("@ss.hasPermi('audit:carPackage:export')")
    @Log(title = "套餐审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AudCarPackage audCarPackage)
    {
        List<AudCarPackage> list = audCarPackageService.selectAudCarPackageList(audCarPackage);
        ExcelUtil<AudCarPackage> util = new ExcelUtil<AudCarPackage>(AudCarPackage.class);
        util.exportExcel(response, list, "套餐审核数据");
    }

    /**
     * 获取套餐审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('audit:carPackage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(audCarPackageService.selectAudCarPackageById(id));
    }

    /**
     * 新增套餐审核
     */
    @PreAuthorize("@ss.hasPermi('audit:carPackage:add')")
    @Log(title = "套餐审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AudCarPackage audCarPackage)
    {
        return toAjax(audCarPackageService.insertAudCarPackage(audCarPackage));
    }

    @PreAuthorize("@ss.hasPermi('audit:carPackage:add')")
    @Log(title = "套餐审核", businessType = BusinessType.INSERT)
    @PostMapping("/startAuditServiceItem")
    public AjaxResult startAuditServiceItem(@RequestBody AudCarPackageDTO audCarPackageDTO)
    {
        return toAjax(audCarPackageService.startAuditServiceItem(audCarPackageDTO));
    }

    /**
     * 修改套餐审核
     */
    @PreAuthorize("@ss.hasPermi('audit:carPackage:edit')")
    @Log(title = "套餐审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AudCarPackage audCarPackage)
    {
        return toAjax(audCarPackageService.updateAudCarPackage(audCarPackage));
    }




    /**
     * 查询待办套餐审核列表
     */
    @PreAuthorize("@ss.hasPermi('audit:carPackage:todo')")
    @GetMapping("/listToDoCarPackage")
    public TableDataInfo listToDoCarPackage()
    {
        startPage();
        List<AudCarPackage> list = audCarPackageService.listToDoCarPackage();
        return getDataTable(list);
    }

    /**
     * 审核套餐
     */
    @PutMapping("/auditServiceItem")
    public AjaxResult auditServiceItem(@RequestBody AudedCarPackageDTO audedCarPackageDTO)
    {
        audCarPackageService.auditServiceItem(audedCarPackageDTO);
        return AjaxResult.success("审核成功");
    }

    /**
     *
     * 查看流程定义文件和图，响应文件内容
     */
    // 看需求，根据需求需要响应内容来决定处理方法怎么做？
    // 方法写void，由我们开发者来自定义响应内容。响应文件内容，常用于文件下载
    @GetMapping("/queryProcess/{instanceId}")
    public void queryProcess(@PathVariable("instanceId") String instanceId,HttpServletResponse response) throws IOException {
        InputStream in = audCarPackageService.getInputStream(instanceId);  // 把文件内容或者图拿到封装一个 InputStream对象
        FileCopyUtils.copy(in,response.getOutputStream());
    }


    /**
     * 查询已办套餐审核列表
     */
    @GetMapping("/listDoneCarPackage")
    public TableDataInfo listDoneCarPackage()
    {
        startPage();
        List<AudCarPackage> list = audCarPackageService.listDoneCarPackage();
        return getDataTable(list);
    }


    /**
     * 查询审核历史
     */
    @GetMapping("/queryAuditHistories/{instanceId}")
    public AjaxResult queryAuditHistories(@PathVariable("instanceId") String instanceId )
    {
        List<TaskHistoryVO> list = audCarPackageService.queryAuditHistories(instanceId);
        return  AjaxResult.success(list);
    }

    /**
     * 撤销套餐审核
     */
    @Log(title = "撤销套餐审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/cancelCarPackage/{instanceId}")
    public AjaxResult cancelCarPackage(@PathVariable("instanceId") String instanceId)
    {
        audCarPackageService.cancelCarPackage(instanceId);
        return AjaxResult.success("撤销成功");
    }
}
